using System.IO;
using UnityEngine;

namespace MaterialUI
{
	public static class VectorImageManager
	{
		public static int currentPack;

		public const string materialDesignIconsFolderPath = "Assets/MaterialUI/Fonts/Resources/Material Design Icons";

		public const string materialUIIconsFontName = "MaterialUI Icons";

		public const string materialDesignIconsFontName = "Material Design Icons";

		private const string prefFontDestinationFolder = "PREF_FONT_ICON_DESTINATION_FOLDER";

		public static string fontDestinationFolder
		{
			get
			{
				return PlayerPrefs.GetString("PREF_FONT_ICON_DESTINATION_FOLDER", "/Vector Fonts") + "/Resources";
			}
			set
			{
				PlayerPrefs.SetString("PREF_FONT_ICON_DESTINATION_FOLDER", value);
				PlayerPrefs.Save();
			}
		}

		private static bool FontDirExists()
		{
			return Directory.Exists(Application.dataPath + "/" + fontDestinationFolder);
		}

		private static string[] GetFontDirectories(string fontsPath)
		{
			if (FontDirExists())
			{
				return Directory.GetDirectories(fontsPath);
			}
			return new string[0];
		}

		public static string[] GetAllIconSetNames()
		{
			string fontsPath = Application.dataPath + "/" + fontDestinationFolder;
			string[] fontDirectories = GetFontDirectories(fontsPath);
			string[] array = new string[fontDirectories.Length + 2];
			array[0] = "MaterialUI Icons";
			array[1] = "Material Design Icons";
			for (int i = 0; i < fontDirectories.Length; i++)
			{
				array[i + 2] = new DirectoryInfo(fontDirectories[i].Replace("\\", "/")).Name;
			}
			return array;
		}

		public static bool IsMaterialDesignIconFont(string fontName)
		{
			return fontName == "Material Design Icons";
		}

		public static bool IsMaterialUIIconFont(string fontName)
		{
			return fontName == "MaterialUI Icons";
		}

		public static Font GetIconFont(string name)
		{
			return Resources.Load<Font>(name + "/" + name);
		}

		public static VectorImageSet GetIconSet(string name)
		{
			return JsonUtility.FromJson<VectorImageSet>(Resources.Load<TextAsset>(name + "/" + name).text);
		}

		public static string GetIconCodeFromName(string name, string setName = "*")
		{
			if (setName == "*")
			{
				string[] allIconSetNames = GetAllIconSetNames();
				VectorImageSet[] array = new VectorImageSet[allIconSetNames.Length];
				for (int i = 0; i < allIconSetNames.Length; i++)
				{
					array[i] = GetIconSet(allIconSetNames[i]);
				}
				for (int j = 0; j < array.Length; j++)
				{
					for (int k = 0; k < array[j].iconGlyphList.Count; k++)
					{
						if (name == array[j].iconGlyphList[k].name)
						{
							return array[j].iconGlyphList[k].unicode;
						}
					}
				}
			}
			else
			{
				VectorImageSet iconSet = GetIconSet(setName);
				for (int l = 0; l < iconSet.iconGlyphList.Count; l++)
				{
					if (name == iconSet.iconGlyphList[l].name)
					{
						return iconSet.iconGlyphList[l].unicode;
					}
				}
			}
			return null;
		}

		public static string GetIconNameFromCode(string code, string setName = "*")
		{
			if (setName == "*")
			{
				string[] allIconSetNames = GetAllIconSetNames();
				VectorImageSet[] array = new VectorImageSet[allIconSetNames.Length];
				for (int i = 0; i < allIconSetNames.Length; i++)
				{
					array[i] = GetIconSet(allIconSetNames[i]);
				}
				for (int j = 0; j < array.Length; j++)
				{
					for (int k = 0; k < array[j].iconGlyphList.Count; k++)
					{
						if (code == array[j].iconGlyphList[k].unicode)
						{
							return array[j].iconGlyphList[k].name;
						}
					}
				}
			}
			else
			{
				VectorImageSet iconSet = GetIconSet(setName);
				for (int l = 0; l < iconSet.iconGlyphList.Count; l++)
				{
					if (code == iconSet.iconGlyphList[l].unicode)
					{
						return iconSet.iconGlyphList[l].name;
					}
				}
			}
			return null;
		}
	}
}
